import unittest
import datetime

from meercat.base.instance import Instance
from meercat.base.metadata import Metadata
from meercat.base.resource import Resource


class InstanceTest(unittest.TestCase):
    def setUp(self):
        self.metadata = [Metadata(source='testS',format='testF',value='testV1'),
                         Metadata(source='testS',format='testF',value='testV2'),
                         Metadata(source='testS',format='testF',value='testV3'),
                         Metadata(source='testS',format='testF',value='testV4'),
                         Metadata(source='testS',format='testF',value='testV4')]
        
        self.modified = datetime.datetime.now()
        self.instance = Instance(source='testS',
                                 identifier='test1',
                                 active=True,
                                 metadata=self.metadata,
                                 modified=self.modified)
    
    def testEquality(self):
        self.assert_(self.instance == Instance(source='testS',
                                               identifier='test1',
                                               active=True,
                                               metadata=self.metadata,
                                               modified=self.modified))
        self.assertFalse(self.instance == Instance(source='testS',
                                                   identifier='test2',
                                                   active=True,
                                                   metadata=self.metadata,
                                                   modified=self.modified))
        self.assertFalse(self.instance == Instance(source='testS2',
                                                   identifier='test1',
                                                   active=True,
                                                   metadata=self.metadata,
                                                   modified=self.modified))
        self.assertFalse(self.instance == 'garbage')
        
    
    def testAccessors(self):
        self.assertEqual(self.instance.getSource(),'testS')
        self.assertEqual(self.instance.getIdentifier(),'test1')
        self.assert_(self.instance.isActive())
        self.assertEqual(self.instance.getLastModified(),self.modified)
        
        for m in self.metadata:
            self.assert_(m in self.instance.getMetadata())
        self.assertEqual(len(self.instance),4)
        
        for m in self.instance:
            self.assert_(m in self.metadata)
        
    
    def testHashing(self):
        self.assertEqual(self.instance.__hash__(),
                         Instance(source='testS',identifier='test1').__hash__())
    
    def testBadMetadata(self):
        self.assertRaises(TypeError, Instance, source='testS', 
                          identifier='test1', metadata=['value'])
    
    def testBadModified(self):
        self.assertRaises(AssertionError, Instance, source='testS',
                          identifier='test1', modified='2009/08/01')
    
def test_suite():
    suite = unittest.TestSuite()
    suite.addTest(unittest.TestLoader().loadTestsFromTestCase(InstanceTest))
    return suite

if __name__ == '__main__':
    unittest.TextTestRunner(verbosity=2).run(test_suite())